{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Perfect Number"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: checkPerfectNumber"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #完美数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>对于一个&nbsp;<strong>正整数</strong>，如果它和除了它自身以外的所有 <strong>正因子</strong> 之和相等，我们称它为 <strong>「完美数」</strong>。</p>\n",
    "\n",
    "<p>给定一个&nbsp;<strong>整数&nbsp;</strong><code>n</code>，&nbsp;如果是完美数，返回 <code>true</code>；否则返回 <code>false</code>。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>num = 28\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>28 = 1 + 2 + 4 + 7 + 14\n",
    "1, 2, 4, 7, 和 14 是 28 的所有正因子。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>num = 7\n",
    "<strong>输出：</strong>false\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= num &lt;= 10<sup>8</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [perfect-number](https://leetcode.cn/problems/perfect-number/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [perfect-number](https://leetcode.cn/problems/perfect-number/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['28', '7']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        return num==6 or num==28 or num==496 or num==8128 or num==33550336"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num==1:\n",
    "            return False\n",
    "        l = [1]\n",
    "        d = 0\n",
    "        while d * d < num:\n",
    "            d += 1\n",
    "        print(d)\n",
    "        for i in range(2, d + 1):\n",
    "            print(l)\n",
    "            if num % i == 0:\n",
    "                if i not in l:\n",
    "                    l.append(i)\n",
    "                if (num//i) not in l:\n",
    "                    l.append(num // i)\n",
    "        print(l)\n",
    "        return sum(l) == num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num == 1:\n",
    "            return False\n",
    "        i = 2\n",
    "        s = 1\n",
    "        while i <= int(math.sqrt(num)):\n",
    "            if num % i == 0:\n",
    "                if i != num // i:\n",
    "                    s = s + i + num // i\n",
    "                else:\n",
    "                    s = s + i\n",
    "            i += 1\n",
    "        return s == num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num == 1:\n",
    "            return False\n",
    "        zyz = 1\n",
    "        for i in range(2, int(math.sqrt(num)+1)):\n",
    "            if num % i == 0:\n",
    "                zyz += (i + num // i)\n",
    "        if zyz == num:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num == 1:\n",
    "            return False\n",
    "\n",
    "        sum = 1\n",
    "        d = 2\n",
    "        while d * d <= num:\n",
    "            if num % d == 0:\n",
    "                sum += d\n",
    "                if d * d < num:\n",
    "                    sum += num / d\n",
    "            d += 1\n",
    "        return sum == num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num == 1:\n",
    "            return False\n",
    "        yinzi_list = [1, ]\n",
    "        for i in range(2, int(sqrt(num)) + 1):\n",
    "            if num % i == 0:\n",
    "                if num // i == i:\n",
    "                    yinzi_list.append(i)\n",
    "                else:\n",
    "                    yinzi_list.append(i)\n",
    "                    yinzi_list.append(num // i)\n",
    "        return sum(yinzi_list) == num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        p = 2\n",
    "        s = 1\n",
    "        if num == 1: return False\n",
    "        while p * p <= num:\n",
    "            if num % p == 0:\n",
    "                print(p)\n",
    "                s += p + (num // p if p < num // p else 0)\n",
    "            p += 1\n",
    "        return s == num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num == 1:\n",
    "            return False\n",
    "        temp = []\n",
    "        for i in range(1, (int)(sqrt(num))+ 1):\n",
    "            if num % i == 0:\n",
    "                temp += [i, num // i]\n",
    "        temp.remove(num)\n",
    "        return sum(list(set(temp))) == num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num==1:\n",
    "            return False\n",
    "        i=2\n",
    "        ans=1\n",
    "        \n",
    "        while  i<=(num/i):\n",
    "            if num%i==0:\n",
    "                ans+=i\n",
    "                if (i * i != num) :\n",
    "                    ans += num / i\n",
    "            i+=1\n",
    "        return ans==num\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        return num in [6,28,496,8128,33550336]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num == 1:\n",
    "            return False\n",
    "        ans = 0\n",
    "        i = 2\n",
    "        while i*i <= num:\n",
    "            if num%i == 0:\n",
    "                ans += i + num//i \n",
    "            i += 1\n",
    "        ans += 1\n",
    "        if ans == num:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        return num == 6 or num == 28 or num == 496 or num == 8128 or num == 33550336"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\r\n",
    "        if num == 1: return False\r\n",
    "        res = 1\r\n",
    "        mid = int(num ** 0.5) + 1\r\n",
    "        for i in range(2, mid):\r\n",
    "            if num % i == 0:\r\n",
    "                res += i + (num // i)\r\n",
    "        return res == num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num<=1:\n",
    "            return False\n",
    "\n",
    "        res=[]\n",
    "        \n",
    "        for i in range(1,int(math.sqrt(num)) + 1):\n",
    "            if num%i==0:\n",
    "                res.append(i)\n",
    "                res.append(num//i)\n",
    "\n",
    "        res.remove(num)\n",
    "        \n",
    "        return True if num==sum(res) else False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        sum = 1     # 1一定满足,从二开始即可\n",
    "        d = 2\n",
    "        if num==1:\n",
    "            return False\n",
    "        while d*d<=num:\n",
    "            if num%d==0:\n",
    "                sum = sum + d\n",
    "                if d*d<num:\n",
    "                    sum+=num/d\n",
    "            d+=1\n",
    "        return num==sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        sum = 0\n",
    "        if num == 1:\n",
    "            return False\n",
    "\n",
    "        for i in range(math.floor(math.sqrt(num)), 1, -1):\n",
    "            if num % i == 0:\n",
    "                if num // i == i:\n",
    "                    sum += i\n",
    "                else:\n",
    "                    sum += i + num // i\n",
    "            if sum >= num:\n",
    "                return False\n",
    "        \n",
    "        return (sum + 1) == num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        cnt = 0\n",
    "        d = 1\n",
    "        while d*d < num:\n",
    "            if num % d == 0:\n",
    "                cnt += d\n",
    "                if d != 1:\n",
    "                    cnt += num // d\n",
    "            d += 1\n",
    "        return cnt == num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        cut=0\n",
    "        d=1\n",
    "        while d**2 <num:\n",
    "            if num % d==0:\n",
    "                cut+=d\n",
    "                if d!=1:\n",
    "                    cut+=num//d\n",
    "            d+=1\n",
    "        if cut==num:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "   \n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num<=1:\n",
    "            return False\n",
    "        tmp=1\n",
    "        for i in range(2,int(num**0.5)+1):\n",
    "            if num%i==0:\n",
    "                tmp+=i\n",
    "                tmp+=num//i\n",
    "        return tmp==num\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num==1:\n",
    "            return False\n",
    "        s=1\n",
    "        for i in range(2,int(sqrt(num))+1):\n",
    "            if num%i==0:\n",
    "                s+=i+num//i\n",
    "        return num==s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num==1:\n",
    "            return False\n",
    "        sum=1\n",
    "        d=2\n",
    "        while d*d<=num:\n",
    "            if num%d==0:\n",
    "                sum+=d\n",
    "                if d*d<num:\n",
    "                    sum+=num/d\n",
    "            d+=1\n",
    "        return sum==num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        # 真因子如何求呢？\n",
    "        # arr = []\n",
    "        # for i in range(1, num//2+1):\n",
    "        #     if num % i == 0:\n",
    "        #         arr.append(i)\n",
    "        # return sum(arr) == num\n",
    "        if num == 1:\n",
    "            return False\n",
    "        \n",
    "        sum = 1\n",
    "        d = 2\n",
    "        while d * d <= num:\n",
    "            if num % d == 0:\n",
    "                sum += d\n",
    "                if d * d < num:\n",
    "                    sum += num / d\n",
    "            d += 1\n",
    "        return sum == num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num == 1:\n",
    "            return False\n",
    "\n",
    "        sum = 1\n",
    "        d = 2\n",
    "        while d * d <= num:\n",
    "            if num % d == 0:\n",
    "                sum += d\n",
    "                if d * d < num:\n",
    "                    sum += num / d\n",
    "            d += 1\n",
    "        return sum == num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num%2!=0: return False\n",
    "        n=1\n",
    "        i=2\n",
    "        while i<=sqrt(num):\n",
    "            if num%i==0: n+=(i+num/i)\n",
    "            i=i+1\n",
    "        if n==num: return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num == 1:\n",
    "            return False\n",
    "        tmp = [1]\n",
    "        for i in range(2,int(num ** 0.5) + 1):\n",
    "            if num % i == 0:\n",
    "                tmp.append(i)\n",
    "                tmp.append(num // i)\n",
    "        return sum(tmp) == num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num == 1:\n",
    "            return False\n",
    "        a = 1\n",
    "        for i in range(2, isqrt(num) + 1):\n",
    "            if num % i == 0:\n",
    "                a += i + num // i\n",
    "        return num == a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        cnt = 0\n",
    "        d = 1\n",
    "        while d*d < num:\n",
    "            if num % d == 0:\n",
    "                cnt += d\n",
    "                if d != 1:\n",
    "                    cnt += num // d\n",
    "            d += 1\n",
    "        return cnt == num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        temp_sum = 1\n",
    "\n",
    "        if num==1:\n",
    "            return False\n",
    "\n",
    "        for i in range(2,num+1):\n",
    "            if i*i > num:\n",
    "                return num==temp_sum\n",
    "            \n",
    "            if num%i ==0:\n",
    "                temp_sum = temp_sum + i + int(num/i)\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        return num in {6, 28, 496, 8128, 33550336, 8589869056}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        m=num\n",
    "        if num==1:\n",
    "            return False\n",
    "        for i in range(2,int(num**0.5)+1):\n",
    "            if num%i==0:\n",
    "                m-=i+num//i                \n",
    "        m-=1\n",
    "        if m==0:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        lst = []\n",
    "        for n in range(1, int(num ** 0.5)+1):\n",
    "            if num % n == 0:\n",
    "                lst.append(n)\n",
    "                lst.append(num/n)\n",
    "        a = 0\n",
    "        for n in lst:\n",
    "            if n != num:\n",
    "                a += n\n",
    "        return int(a) == num \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num == 1:\n",
    "            return False\n",
    "        res = 0\n",
    "        for i in range(2, int(num**0.5)+1):\n",
    "            if num % i == 0:\n",
    "                res += i\n",
    "                res += num/i\n",
    "        res += 1\n",
    "        if res == num:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "class Solution:\n",
    "\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num == 1:\n",
    "            return False\n",
    "\n",
    "        return num == sum(self.getFactors(num))\n",
    "    \n",
    "    def getFactors(self, num):\n",
    "        end = int(math.ceil(math.sqrt(num)))\n",
    "\n",
    "        factors = [1]\n",
    "\n",
    "        for i in range(2, end):\n",
    "            if num%i == 0:\n",
    "                if i not in factors:\n",
    "                    factors.append(i)\n",
    "                if int(num/i) not in factors:\n",
    "                    factors.append(int(num/i))\n",
    "        \n",
    "        return factors\n",
    "                \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        a1=[]\n",
    "        for i in range(2,int(num**(1/2))+1):\n",
    "            if num%i==0 and i not in a1:\n",
    "                a1.append(i)\n",
    "                if num/i not in a1:\n",
    "                    a1.append(num/i)\n",
    "        if num==1:\n",
    "            return False\n",
    "        return sum(a1)+1==num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        s = 0\n",
    "        if num == 1:\n",
    "            return False\n",
    "        for i in range(1,int(num**0.5)+1):\n",
    "            if num % i == 0:\n",
    "                s += i\n",
    "                if i > 1:\n",
    "                    s += num / i\n",
    "        return s == num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        return num in {6, 28, 496, 8128, 33550336}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num == 1: return False\n",
    "        return num == 1 + sum(x + num // x for x in range(2, int(sqrt(num)) + 1) if num % x == 0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num == 1:\n",
    "            return False\n",
    "\n",
    "        sum = 1\n",
    "        d = 2\n",
    "        while d * d <= num:\n",
    "            if num % d == 0:\n",
    "                sum += d\n",
    "                if d * d < num:\n",
    "                    sum += num / d\n",
    "            d += 1\n",
    "        return sum == num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        return num in {6, 28, 496, 8128, 33550336}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num == 1:\n",
    "            return False\n",
    "        sum = 1\n",
    "        d = 2\n",
    "        while d * d <= num:\n",
    "            if num % d == 0:\n",
    "                sum += (d + num // d) \n",
    "            d += 1 \n",
    "        return sum == num\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num<6:return False\n",
    "        factors = [1]\n",
    "        bound = num//2\n",
    "        i = 2\n",
    "        while i<bound:\n",
    "            if num%i==0:\n",
    "                factors.extend([i,num//i])\n",
    "                bound = num//i\n",
    "            i += 1\n",
    "        return num==sum(factors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num == 1:\n",
    "            return False\n",
    "        d = 2\n",
    "        sum = 1\n",
    "        while d *d <=num:\n",
    "            if num % d ==0:\n",
    "                sum += d\n",
    "                sum += num //d\n",
    "                d += 1\n",
    "            else:\n",
    "                d += 1\n",
    "        return sum== num "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        i = 1\n",
    "        sum = 0\n",
    "        while i <=num ** (1 / 2):\n",
    "            if num % i == 0:\n",
    "                sum+=i+num/i\n",
    "            i+=1\n",
    "        if num==1:\n",
    "            return False\n",
    "        else:\n",
    "            return sum==2*num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        def isPrime(x):\n",
    "            for j in range(2, x//2):\n",
    "                if not x % j:\n",
    "                    return False\n",
    "            return x > 1\n",
    "\n",
    "        # 2^(p-1) * (2^p - 1)\n",
    "        p = 1\n",
    "        while not num % 2:\n",
    "            num //= 2\n",
    "            p += 1\n",
    "        return num + 1 == pow(2, p) and isPrime(p) and isPrime(num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num == 1:\n",
    "            return False\n",
    "        \n",
    "        sum = 1\n",
    "        d = 2\n",
    "        while d * d <= num:\n",
    "            if num % d == 0:\n",
    "                sum += d\n",
    "                if d * d < num:\n",
    "                    sum += num/d\n",
    "            d += 1\n",
    "        return sum == num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        a=1\n",
    "        if num==1:\n",
    "            return False\n",
    "        for i in range(2,int(num**0.5)+1):\n",
    "            if num/i-int(num/i)==0:\n",
    "                a+=(i+num//i)\n",
    "        return a==num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num <= 1: return False\n",
    "        s = 1\n",
    "        x = num\n",
    "        top = int(sqrt(num+1))\n",
    "        for m in range(2, top+1):\n",
    "            if num % m == 0:\n",
    "                s += m\n",
    "                if num//m != m:\n",
    "                    s += num//m\n",
    "        return s == num\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num==1:return False\n",
    "        l=[]\n",
    "        for i in range(2,int(num**0.5)+1):\n",
    "            if num%i==0:\n",
    "                l=l+[i,num//i]\n",
    "        if sum(l)+1==num:return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        s=[]\n",
    "        if num==1:\n",
    "            return False\n",
    "        for i in range(2,int(num**0.5)+1):\n",
    "            if num%i==0:\n",
    "                s.append(i+num//i)               \n",
    "        if not(num-1-sum(s)):\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        return num == 6 or num == 28 or num == 496 or num == 8128 or num == 33550336\n",
    "        a=1\n",
    "        if num==1:\n",
    "            return False\n",
    "        for i in range(2,int(num**0.5)+1):\n",
    "            if num/i-int(num/i)==0:\n",
    "                a+=(i+num//i)\n",
    "        return a==num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        ans=0\n",
    "        if num==1:\n",
    "            return False\n",
    "        for i in range (1,int(sqrt(num)+1)):\n",
    "            if num%i==0:\n",
    "                ans+=i+num//i\n",
    "        ans-=num\n",
    "        return ans==num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        return num in {6, 28, 496, 8128, 33550336}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        res=[]\n",
    "        if num==1:return False\n",
    "        sum=0\n",
    "        for i in range(1,int(sqrt(num))+1):\n",
    "            ### 3*3\n",
    "            if num%i==0 and num:\n",
    "                sum+=i\n",
    "                if (num//i)!=i and (num//i)!=num:\n",
    "                    sum+=(num//i)\n",
    "        return sum==num\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num == 1:\n",
    "            return False\n",
    "        sumelem = 1\n",
    "        root = math.ceil(num**0.5)\n",
    "        for element in range(2,root):\n",
    "            if num % element == 0:\n",
    "                sumelem = sumelem + element + (num // element)\n",
    "        \n",
    "        return sumelem == num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        cnt = 0\n",
    "        d = 1\n",
    "        while d*d < num:\n",
    "            if num % d == 0:\n",
    "                cnt += d\n",
    "                if d != 1:\n",
    "                    cnt += num // d\n",
    "            \n",
    "            d += 1\n",
    "        return cnt == num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        # n = 0\n",
    "        # for i in range(1, num):         # 超时\n",
    "        #     if num % i == 0:\n",
    "        #         n += i\n",
    "        # return n == num\n",
    "        if num == 1:\n",
    "            return False\n",
    "        a = [i for i in range(1, int(sqrt(num)) + 1) if num % i == 0]\n",
    "        suml = sum(a)\n",
    "        for i in a:\n",
    "            c = num // i\n",
    "            if c != num and c not in a:     #for中别用列表，会内存不够\n",
    "                suml += c\n",
    "        return suml == num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        res = set()\n",
    "        for i in range(1, int(pow(num, 0.5) + 1)):\n",
    "            if num % i == 0:\n",
    "                res.add(i)\n",
    "                res.add(num // i)\n",
    "        return num * 2 == sum(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num==1:\n",
    "            return False\n",
    "        res=1\n",
    "        for i in range(2,int(sqrt(num)+1)):\n",
    "            if num%i==0:\n",
    "                res+=i+num//i\n",
    "        return True if res==num else False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num == 1:\n",
    "            return False\n",
    "        # 定义一个列表，存储该数的所有正因子\n",
    "        list = []\n",
    "        list.append(1) # 1必然是正因子\n",
    "        # 寻找每个正因子\n",
    "        for i in range(2,int(pow(num,0.5))+1) :\n",
    "            if num % i == 0 :\n",
    "                list.append(i)\n",
    "                list.append(num//i)\n",
    "                print(list)\n",
    "        # 计算列表因素的和\n",
    "        if sum(list) == num :\n",
    "            return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"读题目\n",
    "    1.  对于一个正整数，如果它除了自身以外所有正因子之和相等，我们称它为【完美数】\n",
    "    2.  \n",
    "\n",
    "\"\"\"\n",
    "# 整因数总是成对出现。\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        # 暴力\n",
    "        count = 0\n",
    "        count += 1 # 因子1\n",
    "        for i in range(2, int(num ** 0.5) + 1):\n",
    "            if num % i == 0: # i是num的因子\n",
    "                count += i\n",
    "                count += num // i\n",
    "        return count == num if num != 1 else False\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num == 1:\n",
    "            return False\n",
    "        sum = 1\n",
    "        for i in range(2, int(math.sqrt(num)) + 1):\n",
    "            if num % i == 0:\n",
    "                sum += i\n",
    "                if i != num / i:\n",
    "                    sum += num / i\n",
    "        if sum == num:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        # res = set()\n",
    "        # for i in range(1, int(pow(num, 0.5) + 1)):\n",
    "        #     if num % i == 0:\n",
    "        #         res.add(i)\n",
    "        #         res.add(num // i)\n",
    "        # return num * 2 == sum(res)\n",
    "\n",
    "        return num == 6 or num == 28 or num == 496 or num == 8128 or num == 33550336"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        return num in {6, 28, 496, 8128, 33550336, 8589869056}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        return num==6 or num==28 or num==496 or num==8128 or num==33550336"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num == 1:\n",
    "            return False\n",
    "        sum = 1\n",
    "        d = 2\n",
    "        while d * d <= num:\n",
    "            if num % d == 0:\n",
    "                sum += d\n",
    "                if d * d < num:\n",
    "                    sum += num / d\n",
    "            d += 1\n",
    "        return sum == num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num<6:return False\n",
    "        facsum = 1\n",
    "        i = 2\n",
    "        while i*i<=num:\n",
    "            if num%i==0:\n",
    "                facsum += (i+num//i if i*i!=num else i)\n",
    "            i += 1\n",
    "        return num==facsum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num==1:\n",
    "            return False\n",
    "        a=int(num**0.5)\n",
    "        b=[i for i in range(1,a+1) if num%i==0]\n",
    "        ans=sum(b)\n",
    "        for i in b:\n",
    "            c=num//i\n",
    "            if c!=num:\n",
    "                ans+=c\n",
    "        return ans==num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        if num == 1:\n",
    "            return False\n",
    "\n",
    "        sum = 1\n",
    "        d = 2\n",
    "        while d * d <= num:\n",
    "            if num % d == 0:\n",
    "                sum += d\n",
    "                if d * d < num:\n",
    "                    sum += num / d\n",
    "            d += 1\n",
    "        return sum == num\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        start = int(sqrt(num))\n",
    "        ans = 0\n",
    "        for i in range(start, 0, -1):\n",
    "            if num % i == 0:\n",
    "                a1 = i if i != num else 0\n",
    "                a2 = num // i if num // i != num else 0\n",
    "                ans += a1 + a2\n",
    "        return ans == num  # 去除它自身\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        # res = set()\n",
    "        # for i in range(1, int(pow(num, 0.5) + 1)):\n",
    "        #     if num % i == 0:\n",
    "        #         res.add(i)\n",
    "        #         res.add(num // i)\n",
    "        # return num * 2 == sum(res)\n",
    "\n",
    "        if num == 1:\n",
    "            return False\n",
    "        \n",
    "        s, d = 1, 2\n",
    "        while d * d <= num:\n",
    "            if num % d == 0:\n",
    "                s += d\n",
    "                if d * d < num:\n",
    "                    s += num / d\n",
    "            d += 1\n",
    "        return s == num\n",
    "\n",
    "        # return num == 6 or num == 28 or num == 496 or num == 8128 or num == 33550336"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkPerfectNumber(self, num: int) -> bool:\n",
    "        # n = 0\n",
    "        # for i in range(1, num):         # 超时\n",
    "        #     if num % i == 0:\n",
    "        #         n += i\n",
    "        # return n == num\n",
    "        if num == 1:\n",
    "            return False\n",
    "        a = [i for i in range(1, int(sqrt(num)) + 1) if num % i == 0]\n",
    "        suml = sum(a)\n",
    "        for i in a:\n",
    "            c = num // i\n",
    "            if c != num and c not in a:\n",
    "                suml += c\n",
    "        return suml == num"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
